home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
print
/
gs261sr1.zip
/
GSCIE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-18
|
15KB
|
432 lines
/* Copyright (C) 1992, 1993 Aladdin Enterprises. All rights reserved.
This file is part of Ghostscript.
Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the Ghostscript General Public License for full details.
Everyone is granted permission to copy, modify and redistribute
Ghostscript, but only under the conditions described in the Ghostscript
General Public License. A copy of this license is supposed to have been
given to you along with Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies. */
/* gscie.c */
/* CIE color rendering for Ghostscript */
#include "gx.h"
#include "gserrors.h"
#include "gxrefct.h" /* early for gscie.h */
#include "gxcolor.h" /* early for gscolor2.h */
#include "gscspace.h"
#include "gscolor2.h" /* for gs_set/currentcolorrendering */
#include "gscie.h"
#include "gxarith.h"
#include "gxdevice.h" /* for gs_color_index */
#include "gzcolor.h"
#include "gzstate.h"
/* Forward references */
private void near cie_mult3(P3(const gs_vector3 *, const gs_matrix3 *, gs_vector3 *));
private void near cie_matrix_mult3(P3(const gs_matrix3 *, const gs_matrix3 *, gs_matrix3 *));
private void near cie_invert3(P2(const gs_matrix3 *, gs_matrix3 *));
private void near cie_restrict3(P3(const gs_vector3 *, const gs_range3 *, gs_vector3 *));
private void near cie_lookup3(P3(const gs_vector3 *, const gx_cie_cache *, gs_vector3 *));
private void near cie_matrix_init(P1(gs_matrix3 *));
#define lookup(vin, pcache, vout)\
if ( (pcache)->is_identity ) vout = (vin);\
else vout = (pcache)->values[(int)(((vin) - (pcache)->base) * (pcache)->factor)]
#define restrict(vin, range, vout)\
if ( (vin) < (range).rmin ) vout = (range).rmin;\
else if ( (vin) > (range).rmax ) vout = (range).rmax;\
else vout = (vin)
/* ------ Default values for CIE dictionary elements ------ */
/* Default transformation procedures. */
private int
a_identity(const float *in, const gs_cie_a *pcie, float *out)
{ *out = *in;
return 0;
}
private int
abc_identity(const gs_vector3 *in, const gs_cie_abc *pcie, gs_vector3 *out)
{ *out = *in;
return 0;
}
private int
common_identity(const gs_vector3 *in, const gs_cie_common *pcie, gs_vector3 *out)
{ *out = *in;
return 0;
}
private int
render_identity(const gs_vector3 *in, const gs_cie_render *pcie, gs_vector3 *out)
{ *out = *in;
return 0;
}
private int
tpqr_identity(const gs_vector3 *in, const gs_cie_wbsd *pwbsd, const gs_cie_render *pcie, gs_vector3 *out)
{ *out = *in;
return 0;
}
private int
render_table_identity(const byte *in, int m, const gs_cie_render *pcie, float *out)
{ int j;
for ( j = 0; j < m; j++ ) out[j] = in[j] / 255.0;
return 0;
}
/* Default vectors and matrices. */
const gs_range3 Range3_default = { {0,1}, {0,1}, {0,1} };
const gs_cie_abc_proc3 DecodeABC_default = abc_identity;
const gs_cie_common_proc3 DecodeLMN_default = common_identity;
const gs_matrix3 Matrix3_default = { {1,0,0}, {0,1,0}, {0,0,1}, 1 };
const gs_range RangeA_default = {0,1};
const gs_cie_a_proc DecodeA_default = a_identity;
const gs_vector3 MatrixA_default = { 1, 1, 1 };
const gs_vector3 BlackPoint_default = { 0, 0, 0 };
const gs_cie_render_proc3 Encode_default = render_identity;
const gs_cie_transform_proc3 TransformPQR_default = tpqr_identity;
const gs_cie_render_table_proc RenderTableT_default = render_table_identity;
/* setcolorrendering */
int
gs_setcolorrendering(gs_state *pgs, gs_cie_render *pcie)
{ int code = gs_cie_render_init(pcie);
if ( code < 0 ) return code;
rc_assign(pgs->cie_render, pcie, pgs->memory_procs,
"gs_setcolorrendering");
/* Initialize the joint caches, if needed, */
/* by re-installing the color space. */
(*pgs->color_space->type->install_cspace)(pgs->color_space, pgs);
return gx_remap_color(pgs);
}
/* currentcolorrendering */
const gs_cie_render *
gs_currentcolorrendering(const gs_state *pgs)
{ return pgs->cie_render;
}
/* Get the joint caches, to avoid having to import gzstate.h */
gx_cie_joint_caches *
gx_currentciecaches(gs_state *pgs)
{ return pgs->cie_joint_caches;
}
/* ------ Complete a rendering structure ------ */
int
gs_cie_render_init(gs_cie_render *pcie)
{ gs_matrix3 PQR_inverse;
cie_matrix_init(&pcie->MatrixLMN);
cie_matrix_init(&pcie->MatrixABC);
cie_matrix_init(&pcie->MatrixPQR);
cie_invert3(&pcie->MatrixPQR, &PQR_inverse);
cie_matrix_mult3(&PQR_inverse, &pcie->MatrixLMN, &pcie->MatrixPQR_inverse_LMN);
cie_mult3(&pcie->points.WhitePoint, &pcie->MatrixPQR, &pcie->wdpqr);
cie_mult3(&pcie->points.BlackPoint, &pcie->MatrixPQR, &pcie->bdpqr);
/****** FINISH ******/
return 0;
}
/* ------ Fill in the joint cache ------ */
int
gx_cie_joint_caches_init(gx_cie_joint_caches *pjc,
const gs_cie_common *pcie, const gs_cie_render *pcier)
{ pjc->points_sd.ws.xyz = pcie->points.WhitePoint;
cie_mult3(&pjc->points_sd.ws.xyz, &pcier->MatrixPQR, &pjc->points_sd.ws.pqr);
pjc->points_sd.bs.xyz = pcie->points.BlackPoint;
cie_mult3(&pjc->points_sd.bs.xyz, &pcier->MatrixPQR, &pjc->points_sd.bs.pqr);
pjc->points_sd.wd.xyz = pcier->points.WhitePoint;
pjc->points_sd.wd.pqr = pcier->wdpqr;
pjc->points_sd.bd.xyz = pcier->points.BlackPoint;
pjc->points_sd.bd.pqr = pcier->bdpqr;
/****** FINISH ******/
return 0;
}
/* ------ Remap (render) a CIE color (using the caches). ------ */
private int near cie_remap_finish(P4(const gs_vector3 *,
const gs_cie_common *, gx_device_color *, gs_state *));
/* Render a CIEBasedABC color. */
int
gx_remap_CIEBasedABC(const gs_client_color *pc, const gs_color_space *pcs,
gx_device_color *pdc, gs_state *pgs)
{ const gs_cie_abc *pcie = pcs->params.abc;
gs_vector3 abc, lmn;
cie_restrict3((const gs_vector3 *)&pc->paint.values[0], &pcie->RangeABC, &abc);
/* (*pcie->DecodeABC)(&abc, pcie, &abc); */
cie_lookup3(&abc, &pcie->caches.DecodeABC[0], &abc);
cie_mult3(&abc, &pcie->MatrixABC, &lmn);
return cie_remap_finish(&lmn, &pcie->common, pdc, pgs);
}
/* Render a CIEBasedA color. */
int
gx_remap_CIEBasedA(const gs_client_color *pc, const gs_color_space *pcs,
gx_device_color *pdc, gs_state *pgs)
{ const gs_cie_a *pcie = pcs->params.a;
const gx_cie_cache *pcache = &pcie->caches.DecodeA;
float a;
gs_vector3 lmn;
restrict(pc->paint.values[0], pcie->RangeA, a);
/* (*pcie->DecodeA)(&a, pcie, &a); */
lookup(a, pcache, a);
lmn.u = a * pcie->MatrixA.u;
lmn.v = a * pcie->MatrixA.v;
lmn.w = a * pcie->MatrixA.w;
return cie_remap_finish(&lmn, &pcie->common, pdc, pgs);
}
/* Common rendering code. */
private int near
cie_remap_finish(const gs_vector3 *plmn, const gs_cie_common *pcommon,
gx_device_color *pdc, gs_state *pgs)
{ const gs_cie_render *pcie = pgs->cie_render;
const byte **table;
gs_vector3 abc, lmn, xyz, pqr;
gs_client_color cc;
gs_color_space cs;
/* Finish decoding. */
cie_restrict3(plmn, &pcommon->RangeLMN, &lmn);
/* (*pcommon->DecodeLMN)(&lmn, pcommon, &lmn); */
cie_lookup3(&lmn, &pcommon->caches.DecodeLMN[0], &lmn);
cie_mult3(&lmn, &pcommon->MatrixLMN, &xyz);
/* Render. */
if ( pcie == 0 ) /* default rendering */
{ abc = xyz;
table = 0;
}
else
{ const gx_cie_joint_caches *pjc = pgs->cie_joint_caches;
cie_mult3(&xyz, &pcie->MatrixPQR, &pqr);
cie_restrict3(&pqr, &pcie->RangePQR, &pqr);
/* (*pcie->TransformPQR)(&pqr, &pjc->points_sd, pcie, &pqr); */
cie_lookup3(&pqr, &pjc->TransformPQR[0], &pqr);
cie_mult3(&pqr, &pcie->MatrixPQR_inverse_LMN, &lmn);
/* (*pcie->EncodeLMN)(&lmn, pcie, &lmn); */
cie_lookup3(&lmn, &pcie->caches.EncodeLMN[0], &lmn);
cie_restrict3(&lmn, &pcie->RangeLMN, &lmn);
cie_mult3(&lmn, &pcie->MatrixABC, &abc);
/* (*pcie->EncodeABC)(&abc, pcie, &abc); */
cie_look